package com.zjitc.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjitc.common.exception.AuthException;
import com.zjitc.common.utils.RespBean;
import com.zjitc.serviceedu.entity.EduRole;
import com.zjitc.serviceedu.entity.vo.RoleVo;
import com.zjitc.serviceedu.service.EduRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵诚挚
 * @since 2024-10-26
 */
@RestController
@RequestMapping("/serviceedu/edu-role")
@Api(tags = "角色功能")
public class EduRoleController {
    @Autowired
    private EduRoleService eduRoleService;



    @PostMapping("addRole")
    @ApiOperation("添加角色")
    public RespBean addRole(EduRole eduRole) {
        boolean result = eduRoleService.save(eduRole);
        if(!result) {
            throw new AuthException("添加失败");
        }

        return RespBean.success().msg("添加成功");
    }

    @ApiOperation("修改详情查询")
    @GetMapping("getById/{id}")
    public RespBean getById(@PathVariable("id") Long id) {
        EduRole eduRole = eduRoleService.getById(id);
        if(eduRole == null) {
            throw new AuthException("查询失败");
        }

        return RespBean.success().data("data", eduRole).msg("查询成功");
    }

    @ApiOperation("修改角色")
    @PutMapping("updateRole")
    public RespBean updateRole(EduRole eduRole) {
        boolean result = eduRoleService.updateById(eduRole);
        if(!result) {
            throw new AuthException("修改失败");
        }

        return RespBean.success().msg("修改成功");
    }

    @ApiOperation("删除角色")
    @DeleteMapping("delRole/{id}")
    public RespBean delRole(@PathVariable("id") Long id) {
        boolean result = eduRoleService.removeById(id);
        if(!result) {
            throw new AuthException("删除失败");
        }

        return RespBean.success().msg("删除成功");
    }

    @ApiOperation("批量删除角色")
    @DeleteMapping("delMoreRole")
    public RespBean delMoreRole(Long[] ids) {
        boolean result = eduRoleService.removeByIds(Arrays.asList(ids));
        if(!result) {
            throw new AuthException("批量删除失败");
        }

        return RespBean.success().msg("批量删除成功");
    }

    @ApiOperation("条件查询")
    @GetMapping("getPageBatchs")
    public RespBean getPageBatchs(RoleVo roleVo) {

        Page<EduRole> page = new Page<>(roleVo.getPage(), roleVo.getSize());

        QueryWrapper<EduRole> queryWrapper = new QueryWrapper<>();

        if(!StringUtils.isEmpty(roleVo.getName())) {
            queryWrapper.like("name", roleVo.getName());
        }

        eduRoleService.page(page, queryWrapper);

        if(page == null) {
            throw new AuthException("查询失败");
        }

        List<EduRole> list = page.getRecords();
        Long total = page.getTotal();

        return RespBean.success().data("list", list).data("total", total).msg("查询成功");

    }
}

